package com.example.Gethome.repository;

import com.example.Gethome.entity.Home;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface HomeRepository extends JpaRepository<Home, Integer> {

    // 更改房屋的审核状态，可以更改为审核通过或者审核未通过
    @Transactional // 支持事务管理
    @Modifying(clearAutomatically = true) // 清理实体类中的数据
    @Query(value = "update Home set home_verify = ?1 where home_name = ?2")
    void updateVerifyByHomename(String home_verify, String home_name);

    // 选择房屋的审核字段为未审核的房屋，给管理员进行审核
    @Transactional
    @Query(value = "select h from Home h where h.home_verify = '未审核'")
    List<?> findByHomeVerify();

    // 选择房屋的属性，暂定为根据房子的出售属性（租房还是卖房）来进行查找，方便用户的搜索
    // 首先得是审核通过的才可以
    @Transactional
    @Query(value = "select h from Home h where h.home_verify = '审核通过' and h.home_out_type = ?1")
    List<?> findByHomeoutype(String homeouttype);

    // 用户发布房子之后需要查看自己房子是否通过审核
    // 查看房子的所有属性
    @Transactional
    @Query(value = "select h from Home h where h.name = ?1")
    List<?> findByName(String ownerName);

    // 给房子增加一个主人233
    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(value = "update Home set home_owner = ?1 where home_name = ?2")
    void updateHomeownerByHomename(String homeOwner, String homeName);

    // 查看所有当前有主的房子的情况（根据租房还是买房）
    @Transactional
    @Query(value = "select h from Home h where h.home_owner = ?1 and h.home_out_type = ?2")
    List<?> checkHomeByHomeoutype(String homeOwner, String homeOutType);

    // 根据房屋名称来查询房子的信息
    @Transactional
    @Query(value = "select h from Home h where h.home_name = ?1")
    Home findByHomename(String homeName);

    // 根据房屋名称来添加图片路径
    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(value = "update Home set picture = ?1 where home_name = ?2")
    void updateHomepictureByHomename(String picture, String homeName);

}
